package GUI.AddBooking;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

import GUI.Controller.Controller;
import GUI.TabPanels.BookingPanel;

@SuppressWarnings("serial")
public class AddBookingPanel extends JPanel{
	private BookingPanel booking_panel;
	private JPanel east_panel;
	private JPanel west_panel;

	private JPanel top_east_panel;
	private JPanel top_west_panel;
	private JPanel middle_east_panel;
	private JPanel middle_west_panel;
	private JPanel bottom_east_panel;
	private JPanel bottom_west_panel;

	private JButton cancel_button;
	private JButton view_rooms_button;

	private JLabel customer_info;
	private JLabel booking_info;

	private JLabel customer_firstname_label;
	private JTextField customer_firstname_text;

	private JLabel customer_lastname_label;
	private JTextField customer_lastname_text;

	private JLabel customer_email_label;
	private JTextField customer_email_text;

	private JLabel customer_password_label;
	private JPasswordField customer_password_text;

	private JLabel customer_address_label;
	private JTextField customer_address_text;

	private JLabel customer_phno_label;
	private JTextField customer_phno_text;

	private JLabel credit_card_label;
	private JTextField credit_card_text;

	private JLabel customer_subscription_label;
	private JCheckBox customer_subscription_checkbox;

	private JButton create_customer_account;
	private JButton load_customer_account;

	private JLabel num_people_label;
	private JComboBox num_people_box;

	private JPanel start_day_panel;
	private JLabel start_day_label;
	private JComboBox start_day_box;
	private JComboBox start_month_box;
	private JComboBox start_year_box;

	private String [] days = {"01","02","03","04","05","06","07","08","09","10",
			"11","12","13","14","15","16","17","18","19","20",
			"21","22","23","24","25","26","27","28","29","30","31"};
	private String [] months = {"1","2","3","4","5","6","7","8","9","10","11","12"};
	private String [] years = {"2012", "2013", "2014", "2015", "2016", "2017"};

	private JPanel end_day_panel;
	private JLabel end_day_label;
	private JComboBox end_day_box;
	private JComboBox end_month_box;
	private JComboBox end_year_box;

	private JLabel hotel_label;
	private JComboBox hotel_box;

	public AddBookingPanel(){
		booking_panel = BookingPanel.getInstance();
		this.setLayout(new BorderLayout(2, 2));
		SetUpEastPanel();
		SetUpWestPanel();
	}

	public void SetUpEastPanel(){
		east_panel = new JPanel();
		this.add(east_panel, BorderLayout.EAST);
		east_panel.setLayout(new BorderLayout());

		SetUpTopEastPanel();
		SetUpMiddleEastPanel();
		SetUpBottomEastPanel();
	}

	public void SetUpTopEastPanel(){
		top_east_panel = new JPanel();
		east_panel.add(top_east_panel,BorderLayout.NORTH);

		booking_info = new JLabel("Booking Info");
		booking_info.setFont(new Font("SansSerif", Font.BOLD, 24));
		booking_info.setForeground(Color.blue);
		top_east_panel.add(booking_info);
	}

	public void SetUpMiddleEastPanel(){
		middle_east_panel = new JPanel();
		east_panel.add(middle_east_panel, BorderLayout.CENTER);
		middle_east_panel.setLayout(new GridLayout(0,2));

		hotel_label = new JLabel("Hotel:");
		middle_east_panel.add(hotel_label);
		hotel_box = new JComboBox(Controller.GetHotels());
		middle_east_panel.add(hotel_box);

		middle_east_panel.add(new JLabel());
		middle_east_panel.add(new JLabel());

		num_people_label = new JLabel("# of people");
		String [] numbers = {"1", "2", "3", "4"};
		num_people_box = new JComboBox(numbers);
		middle_east_panel.add(num_people_label);
		middle_east_panel.add(num_people_box);
		middle_east_panel.add(new JLabel());
		middle_east_panel.add(new JLabel());

		start_day_label = new JLabel("Start Day:");
		middle_east_panel.add(start_day_label);

		start_day_panel = new JPanel();
		start_day_box = new JComboBox(days);
		start_day_panel.add(start_day_box);
		start_month_box = new JComboBox(months);
		start_day_panel.add(start_month_box);
		start_year_box = new JComboBox(years);
		start_day_panel.add(start_year_box);
		middle_east_panel.add(start_day_panel);

		middle_east_panel.add(new JLabel());
		middle_east_panel.add(new JLabel());

		end_day_label = new JLabel("End Day:");
		middle_east_panel.add(end_day_label);

		end_day_panel = new JPanel();
		end_day_box = new JComboBox(days);
		end_day_panel.add(end_day_box);
		end_month_box = new JComboBox(months);
		end_day_panel.add(end_month_box);
		end_year_box = new JComboBox(years);
		end_day_panel.add(end_year_box);
		middle_east_panel.add(end_day_panel);

		middle_east_panel.add(new JLabel());
		middle_east_panel.add(new JLabel());
	}

	public void SetUpBottomEastPanel(){
		bottom_east_panel = new JPanel();
		east_panel.add(bottom_east_panel, BorderLayout.SOUTH);

		cancel_button = new JButton("Cancel");
		cancel_button.addActionListener(new CancelButton());
		bottom_east_panel.add(cancel_button, BorderLayout.SOUTH);
	}

	public void SetUpWestPanel(){
		west_panel = new JPanel();
		this.add(west_panel, BorderLayout.WEST);

		west_panel.setLayout(new BorderLayout());

		SetUpTopWestPanel();
		SetUpMiddleWestPanel();
		SetUpBottomWestPanel();
	}

	public void SetUpTopWestPanel(){
		top_west_panel = new JPanel();
		west_panel.add(top_west_panel,BorderLayout.NORTH);

		customer_info = new JLabel("Customer Info");
		customer_info.setFont(new Font("SansSerif", Font.BOLD, 24));
		customer_info.setForeground(Color.blue);
		top_west_panel.add(customer_info);
	}

	public void SetUpMiddleWestPanel(){
		middle_west_panel = new JPanel();
		west_panel.add(middle_west_panel, BorderLayout.CENTER);
		middle_west_panel.setLayout(new GridLayout(0,2));


		customer_email_label = new JLabel("Email:");
		customer_email_text = new JTextField(15);
		customer_email_text.setEditable(true);
		middle_west_panel.add(customer_email_label);
		middle_west_panel.add(customer_email_text);

		customer_password_label = new JLabel("Password:");
		customer_password_text = new JPasswordField(15);
		customer_password_text.setEditable(true);
		middle_west_panel.add(customer_password_label);
		middle_west_panel.add(customer_password_text);

		middle_west_panel.add(new JLabel());
		middle_west_panel.add(new JLabel());

		create_customer_account = new JButton("Create/Update Account");
		create_customer_account.addActionListener(new CreateCustomerAccountButton());
		middle_west_panel.add(create_customer_account);

		load_customer_account = new JButton("Load Account");
		load_customer_account.addActionListener(new LoadCustomerAccountButton());
		middle_west_panel.add(load_customer_account);

		middle_west_panel.add(new JLabel());
		middle_west_panel.add(new JLabel());

		customer_firstname_label = new JLabel("First Name:");
		customer_firstname_text = new JTextField(15);
		customer_firstname_text.setEditable(true);
		middle_west_panel.add(customer_firstname_label);
		middle_west_panel.add(customer_firstname_text);

		customer_lastname_label = new JLabel("Last Name:");
		customer_lastname_text = new JTextField(15);
		customer_lastname_text.setEditable(true);
		middle_west_panel.add(customer_lastname_label);
		middle_west_panel.add(customer_lastname_text);

		customer_address_label = new JLabel("Address:");
		customer_address_text = new JTextField(15);
		customer_address_text.setEditable(true);
		middle_west_panel.add(customer_address_label);
		middle_west_panel.add(customer_address_text);

		customer_phno_label = new JLabel("Phone Number:");
		customer_phno_text = new JTextField(15);
		customer_phno_text.setEditable(true);
		middle_west_panel.add(customer_phno_label);
		middle_west_panel.add(customer_phno_text);

		credit_card_label = new JLabel("Credit Card Number");
		credit_card_text = new JTextField(15);
		credit_card_text.setEditable(true);
		middle_west_panel.add(credit_card_label);
		middle_west_panel.add(credit_card_text);
	}

	public void SetUpBottomWestPanel(){
		bottom_west_panel = new JPanel();
		west_panel.add(bottom_west_panel, BorderLayout.SOUTH);
		bottom_west_panel.setLayout(new GridLayout(0,2));

		customer_subscription_label = new JLabel("Do you want to subscribe to special offers?");
		customer_subscription_checkbox = new JCheckBox();
		bottom_west_panel.add(customer_subscription_label);
		bottom_west_panel.add(customer_subscription_checkbox);

		bottom_west_panel.add(new JLabel());

		view_rooms_button = new JButton("View Available Rooms");
		view_rooms_button.addActionListener(new ViewAvailableRoomsButton());
		bottom_west_panel.add(view_rooms_button);
	}

	public String[] getBookingDates(){
		String start_day = (String) start_year_box.getSelectedItem()+ "-" +
				(String) start_month_box.getSelectedItem()+ "-" +
				(String) start_day_box.getSelectedItem();
		String end_day = (String) end_year_box.getSelectedItem()+ "-" +
				(String) end_month_box.getSelectedItem()+ "-" +
				(String) end_day_box.getSelectedItem();
		String[] info = {
				start_day,
				end_day,					
		};
		// input[0] -> customerID
		// input[1] -> roomID
		// input[2] -> start_date
		// input[3] -> end_date

		return info;
	}

	public String getHotelID(){
		String hotel_id = (String) hotel_box.getSelectedItem();
		hotel_id = hotel_id.substring(1, hotel_id.indexOf(']'));
		return hotel_id;
	}

	public String[] getCustomerID(){
		@SuppressWarnings("deprecation")
		String [] ret_value =  {customer_email_text.getText(), customer_password_text.getText()};
		return ret_value;
	}

	class ViewAvailableRoomsButton implements ActionListener{

		@SuppressWarnings("deprecation")
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(Controller.GetCustomerID(customer_email_text.getText(), customer_password_text.getText()) != -1){
				try
				{
					Long.parseLong(customer_phno_text.getText());
					Integer.parseInt(credit_card_text.getText());
					if (customer_lastname_text.getText().compareTo("") == 0 
							|| customer_lastname_text.getText().length() < 2
							|| customer_firstname_text.getText().compareTo("") == 0 
							|| customer_firstname_text.getText().length() < 2
							|| customer_password_text.getText().compareTo("") == 0 
							|| customer_password_text.getText().length() < 6
							|| customer_email_text.getText().compareTo("") == 0 
							|| customer_email_text.getText().length() < 2
							|| customer_address_text.getText().compareTo("") == 0 
							|| customer_address_text.getText().length() < 2
							|| customer_phno_text.getText().compareTo("") == 0 
							|| customer_phno_text.getText().length() < 2)
						throw new Exception();
					if (new GregorianCalendar(Integer.valueOf(String.valueOf(start_year_box.getSelectedItem())),
							Integer.valueOf(String.valueOf(start_month_box.getSelectedItem())),
							1 + Integer.valueOf(String.valueOf(start_day_box.getSelectedItem()))).after(
									new GregorianCalendar(Integer.valueOf(String.valueOf(end_year_box.getSelectedItem())),
											Integer.valueOf(String.valueOf(end_month_box.getSelectedItem())),
											Integer.valueOf(String.valueOf(end_day_box.getSelectedItem())))))
						throw new Exception();
					booking_panel.UpdateAvailableRooms();
					booking_panel.ViewRooms();
				}
				catch(NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null, "Phone Number and Credit Card must be integers.");
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null, "Fields cannot be empty or less than 2 characters.\nPasswords must be at least 6 characters long.\nThe Start Date must be before the End Date.");
				}
			}
			else{
				JOptionPane.showMessageDialog(null, "Invalid Email and Password.");
			}
		}
	}

	class CancelButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			booking_panel.ViewBookings();
		}
	}

	class CreateCustomerAccountButton implements ActionListener{

		@SuppressWarnings("deprecation")
		@Override
		public void actionPerformed(ActionEvent arg0) {
		try
		{
			Long.parseLong(customer_phno_text.getText());
			Integer.parseInt(credit_card_text.getText());
			if (customer_lastname_text.getText().compareTo("") == 0 
					|| customer_lastname_text.getText().length() < 2
					|| customer_firstname_text.getText().compareTo("") == 0 
					|| customer_firstname_text.getText().length() < 2
					|| customer_password_text.getText().compareTo("") == 0 
					|| customer_password_text.getText().length() < 6
					|| customer_email_text.getText().compareTo("") == 0 
					|| customer_email_text.getText().length() < 2
					|| customer_address_text.getText().compareTo("") == 0 
					|| customer_address_text.getText().length() < 2
					|| customer_phno_text.getText().compareTo("") == 0 
					|| customer_phno_text.getText().length() < 2)
				throw new Exception();
			if (customer_email_text.getText().length() > 1
					&& customer_password_text.getText().length() > 1
					&& customer_lastname_text.getText().length() > 1
					&& customer_firstname_text.getText().length() > 1
					&& customer_phno_text.getText().length() > 1
					&& customer_address_text.getText().length() > 1)
			{
				String args[] = { 
						customer_lastname_text.getText(),
						customer_firstname_text.getText(),
						customer_password_text.getText(),
						customer_email_text.getText(),
						"0",
						customer_address_text.getText(),
						customer_phno_text.getText(),
						(customer_subscription_checkbox.isSelected() ? "0" : "1")
				};
				Controller.CreateCustomer(args);
				JOptionPane.showMessageDialog(null, "Customer Created/Updated!");
			}
		}
		catch(NumberFormatException e)
		{
			JOptionPane.showMessageDialog(null, "Phone Number and Credit Card must be integers.");
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Fields cannot be empty or less than 2 characters. Passwords must be at least 6 characters long.");
		}
	}
}

class LoadCustomerAccountButton implements ActionListener{

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (customer_email_text.getText().length() > 1 && customer_password_text.getText().length() > 1)
		{
			String args[] = { customer_email_text.getText(), customer_password_text.getText() };
			String[] values = Controller.GetCustomer(args);
			if (values != null && values.length > 4)
			{
				//customerID = values[0];
				customer_firstname_text.setText(values[1]);
				customer_lastname_text.setText(values[2]);
				//customerAccountType = values[3];
				customer_address_text.setText(values[4]);
				customer_phno_text.setText(values[5]);
			}else{
				JOptionPane.showMessageDialog(booking_panel, "Invalid Email or Password",
						"Error",
						JOptionPane.WARNING_MESSAGE);
			}
		}else{
			JOptionPane.showMessageDialog(booking_panel, "Email and Password must be greater than 2 characters",
					"Error",
					JOptionPane.WARNING_MESSAGE);
		}
	}
}
}
